#include "BlockQueue.hpp"
#include <pthread.h>
#include <unistd.h>
#include <ctime>
#include <functional>

void test()
{
    std::cout << "haha test..." << std::endl;
}
void hello()
{
    std::cout << "hehe hello..." << std::endl;
}
using task_t = std::function<void()>;

using namespace BlockQueueModule;



void* Consumer(void* args)
{
    BlockQueue<task_t>* bq = static_cast<BlockQueue<task_t>*>(args);
    while(true)
    {
        task_t t;
        //1.从bq拿到数据
        bq->Pop(&t);
        //2.做处理
        t();
    }
}

void* Productor(void* args)
{
    BlockQueue<task_t>* bq = static_cast<BlockQueue<task_t>*>(args);
    int data = 10;
    while(true)
    {
        sleep(1);
        bq->Equeue(test);
    }
}

int main()
{
    srand(time(nullptr) ^ getpid());
    BlockQueue<task_t>* bq = new BlockQueue<task_t>(5);//共享资源 -> 临界资源
    //单生产,单消费
    pthread_t c1,c2,p1,p2,p3;
    pthread_create(&c1,nullptr,Consumer,bq);
    pthread_create(&c2,nullptr,Consumer,bq);
    pthread_create(&p1,nullptr,Productor,bq);
    pthread_create(&p2,nullptr,Productor,bq);
    pthread_create(&p3,nullptr,Productor,bq);

    pthread_join(c1,nullptr);
    pthread_join(c2,nullptr);
    pthread_join(p1,nullptr);
    pthread_join(p2,nullptr);
    pthread_join(p3,nullptr);

    delete bq;
    return 0;
}
